package com.klikmee.sms.core.base.resolvers;

import com.klikmee.sms.core.base.communicator.DeliveryReport2MessageResolver;
import com.klikmee.sms.core.base.dao.OutgoingMessageDAO;
import com.klikmee.sms.core.base.util.Constants;
import com.klikmee.sms.core.model.OutgoingMessage;

/**
 * Concrete resolver for Vip provider.
 * 
 * @author makcro
 * 
 */
public class VipDeliveryReport2MessageResolverImpl implements DeliveryReport2MessageResolver {

	/**
	 * Ref to the outgoing message dao instance used to find outgoing messages.
	 */
	private OutgoingMessageDAO	outgoingMessageDAO	= null;

	/**
	 * To find MT message first check if
	 * {@link OutgoingMessage#getProvidersMesssageId()} value is present. That
	 * means that this is delivery for the charged message. If not that means
	 * that delivery report for bulk message is received and then it has to be
	 * found using {@link OutgoingMessage#getId()} value.
	 * 
	 * @param deliveryReport
	 *            delivery report info used to find MT message
	 */
	@Override
	public OutgoingMessage resolve(OutgoingMessage deliveryReport) {

		OutgoingMessage message = null;
		// check for providers message id
		String providersMesssageId = deliveryReport.getProvidersMesssageId();
		if (providersMesssageId != null) {
			message = outgoingMessageDAO.findByProvidersMessageIDAndProvider(providersMesssageId, Constants.PROVIDER_VIP);
		}

		// find by message id
		else if (deliveryReport.getId() != null) {
			message = outgoingMessageDAO.findByMessageIDAndProvider(deliveryReport.getId(), Constants.PROVIDER_VIP);
		}
		
		// if no message is found throw exception
		if(message == null){
			
		}
		return message;
	}

	public void setOutgoingMessageDAO(OutgoingMessageDAO outgoingMessageDAO) {
		this.outgoingMessageDAO = outgoingMessageDAO;
	}

}
